const { build, parse } = require('node-xlsx');
const fs = require("fs");
const { resolve } = require('path');
const { validateTrans } = require('../common');

/**
 * 标记翻译文件翻译内容不通过的原因
 * @param {string} fromFileName 
 * @param {string?} toFileName 
 */
exports.validateXls = function (fromFileName = 'i18n.xls', toFileName) {
    if (!fromFileName.endsWith(".xls")) fromFileName = `${fromFileName}.xls`;
    let writePath = resolve(__dirname, '../asset/xls', fromFileName)
    let [{ name, data }] = parse(writePath);
    let realData = data.splice(1);
    let writeData = realData.map(value => {
        return validateTrans(value)
    })

    let buffer = build([
        {
            name,
            data: [
                ...data,
                ...writeData,
            ]
        }
    ])
    if (!!!toFileName) {
        toFileName = fromFileName
    } else {
        if (!toFileName.endsWith('.xls')) {
            toFileName = `${toFileName}.xls`
        }
    }
    fs.writeFileSync(toFileName, buffer)
}

